﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using SapB1WebPOS.Utils;

namespace SapB1WebPOS.DAL
{
    public class BusinessPartnersDAO
    {
        public DataSet GetTop1OCRD(string companyDB)
        {
            try
            {
                DataSet dt = new DataSet("OCRD");
                SQLConnection connect = new SQLConnection(companyDB);
                dt = connect.Excute_Query("SELECT  CardCode FROM OCRD ", null);
                return dt;
            }
            catch (Exception ex)
            {
                Log.WriteException(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "SystemError", ex);
                throw ex;
            }
        }
        public DataSet GetDefaultOCRD(string companyDB)
        {
            try
            {
                DataSet dt = new DataSet("OCRD");
                SQLConnection connect = new SQLConnection(companyDB);
                dt = connect.Excute_Query("SELECT  CardCode,CardName FROM OCRD WHERE CardCode=(SELECT TOP(1) DfltCard FROM OACP)  ", null);
                return dt;
            }
            catch (Exception ex)
            {
                Log.WriteException(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "SystemError", ex);
                throw ex;
            }
        }
        public DataSet GetOCRD(string companyDB,string cardTypeFor)
        {
            try
            {
                DataSet dt = new DataSet("OCRD");
                SQLConnection connect = new SQLConnection(companyDB);
                if (cardTypeFor != "S")
                {
                    dt = connect.Excute_Query("SELECT CardCode, CardName FROM OCRD WHERE CardType <>'S'  ORDER BY CardCode ASC  ", null);
                }
                else
                {
                    dt = connect.Excute_Query("SELECT CardCode, CardName FROM OCRD WHERE CardType = 'S'  ORDER BY CardCode ASC  ", null);
                }
                return dt;
            }
            catch (Exception ex)
            {
                Log.WriteException(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "SystemError", ex);
                throw ex;
            }
        }
        public DataSet GetOCRD(string companyDB, string keySearch, string cardTypeFor)
        {
            try
            {
                DataSet dt = new DataSet("OCRD");
                SQLConnection connect = new SQLConnection(companyDB);
                List<object> lstParamValue = new List<object>();
                lstParamValue.Add(keySearch);
                if (cardTypeFor != "S")
                {
                    dt = connect.Excute_Query("SELECT CardCode, CardName FROM OCRD WHERE CardType <>'S' AND  CardCode LIKE '%' + @Param1 + '%' OR CardName LIKE '%' + @Param1 + '%' ORDER BY CardCode ASC ", lstParamValue);
                }
                else
                {
                    dt = connect.Excute_Query("SELECT CardCode, CardName FROM OCRD WHERE CardType ='S' AND CardCode LIKE '%' + @Param1 + '%' OR CardName LIKE '%' + @Param1 + '%' ORDER BY CardCode ASC ", lstParamValue);
                }


                return dt;
            }
            catch (Exception ex)
            {
                Log.WriteException(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "SystemError", ex);
                throw ex;
            }
        }
        public DataSet GetOCRDByCardCode(string companyDB,string cardCode, string cardTypeFor)
        {
            try
            {
                DataSet dt = new DataSet("OCRD");
                SQLConnection connect = new SQLConnection(companyDB);
                List<object> lstParamValue = new List<object>();
                lstParamValue.Add(cardCode);
                if (cardTypeFor != "S")
                {
                    dt = connect.Excute_Query("SELECT CardCode, CardName FROM OCRD WHERE   CardType <>'S' AND   CardCode = @Param1 ORDER BY CardCode ASC ", lstParamValue);
                }
                else
                {
                    dt = connect.Excute_Query("SELECT CardCode, CardName FROM OCRD WHERE   CardType = 'S' AND   CardCode = @Param1 ORDER BY CardCode ASC ", lstParamValue);
                }
                return dt;
            }
            catch (Exception ex)
            {
                Log.WriteException(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "SystemError", ex);
                throw ex;
            }
        }
        public long CountOCRD(string companyDB, string cardTypeFor)
        {
            try
            {
                DataSet dt = new DataSet("OCRD");
                SQLConnection connect = new SQLConnection(companyDB);
                if (cardTypeFor != "S")
                {
                    dt = connect.Excute_Query("SELECT COUNT(*) AS NumOfBP FROM OCRD WHERE CardType <>'S'", null);
                }
                else
                {
                    dt = connect.Excute_Query("SELECT COUNT(*) AS NumOfBP FROM OCRD WHERE CardType  = 'S'", null);
                }
                if (dt != null && dt.Tables.Count > 0 && dt.Tables[0].Rows.Count > 0)
                {
                    string numOfBP = dt.Tables[0].Rows[0]["NumOfBP"].ToString();
                    if (numOfBP.Length > 0)
                    {
                        return long.Parse(numOfBP);
                    }
                }
                return 0;
            }
            catch (Exception ex)
            {
                Log.WriteException(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "SystemError", ex);
                throw ex;
            }
        }
    }
}